import { _decorator, find, Node } from 'cc';
import { BaseLayer } from './BaseLayer';
import { AudioMgr } from '../manager/AudioMgr';
import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_LAYER } from '../Enum';
import { SdkMgr } from '../manager/SdkMgr';
import { Ins } from '../Ins';
import { EffMgr } from '../manager/EffMgr';
import { DataMgr } from '../manager/DataMgr';
const { ccclass, property } = _decorator;

@ccclass('LoseLayer')
export class LoseLayer extends BaseLayer {
    btn_restart: Node = null
    btn_share: Node = null
    btn_revive: Node = null
    btn_close: Node = null

    onLoad() {
        this.btn_restart = find('panel/main/buttons/btn_restart', this.node)
        this.btn_share = find('panel/main/btn_share', this.node)
        this.btn_revive = find('panel/main/buttons/btn_revive', this.node)
        this.btn_restart.on('click', this.onRestartClick, this)
        this.btn_share.on('click', this.onShareClick, this)
        this.btn_close = find('panel/header/btn_close', this.node)
        this.btn_close.on('click', this.onCloseClick, this)
        this.btn_revive.on('click', this.onReviveClick, this)
    }

    onDestroy() {
        this.btn_restart.off('click', this.onRestartClick, this)
        this.btn_share.off('click', this.onShareClick, this)
        this.btn_close.off('click', this.onCloseClick, this)
        this.btn_revive.on('click', this.onReviveClick, this)
    }

    onEnable() {
        SdkMgr.ins.showInterstitialAd()
    }

    async onRestartClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        this.isFade = false
        await Ins.FadeMgr.fadeIn()
        Ins.UIMgr.toggle(ENUM_UI_LAYER.LOSE, false)
        Ins.GameMgr.onGameStart()
    }

    onShareClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (!SdkMgr.ins.getPlatform()) {
            EffMgr.ins.showTip('EffTip', '该功能仅支持小游戏平台')
            return
        }
        SdkMgr.ins.activeShare()
    }

    onCloseClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataMgr.ins.gameStatus = ENUM_GAME_STATUS.UNACTIVE
        Ins.UIMgr.toggle(ENUM_UI_LAYER.MAIN, false)
        Ins.UIMgr.toggle(ENUM_UI_LAYER.LOSE, false)
        Ins.UIMgr.toggle(ENUM_UI_LAYER.MENU)
    }

    onReviveClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        SdkMgr.ins.showVideoAd(() => {
            DataMgr.ins.countDown = 30
            DataMgr.ins.gameStatus = ENUM_GAME_STATUS.ACTIVE
            Ins.UIMgr.toggle(ENUM_UI_LAYER.LOSE, false)
            Ins.UIMgr.setMainCountDown()
            Ins.GameMgr.onGameCheck()
        }, (str: string) => {
            EffMgr.ins.showTip('EffTip', str)
        })
    }
}


